
#include "st7789v_spi.h"
#include <stdint.h>
#include <math.h>
#include <unistd.h>

#define BLACK 0x0000
#define WHITE 0xFFFF
#define SCREEN_WIDTH 240
#define SCREEN_HEIGHT 320

typedef struct {
    uint16_t x;
    uint16_t y;
    uint16_t width;
    uint16_t height;
    uint16_t radius;
} RoundedRectParams;

void draw_pixel(uint16_t x, uint16_t y, uint16_t color) {
    st7789v_spi_draw_area(x, y, x, y, color);
}

void draw_quarter_circle(uint16_t cx, uint16_t cy, uint16_t r, uint8_t quadrant, uint16_t color) {
    int x = r;
    int y = 0;
    int err = 0;
    while (x >= y) {
        switch(quadrant) {
            case 1: draw_pixel(cx + x, cy - y, color);
                    draw_pixel(cx + y, cy - x, color); break;
            case 2: draw_pixel(cx - y, cy - x, color);
                    draw_pixel(cx - x, cy - y, color); break;
            case 3: draw_pixel(cx - x, cy + y, color);
                    draw_pixel(cx - y, cy + x, color); break;
            case 4: draw_pixel(cx + y, cy + x, color);
                    draw_pixel(cx + x, cy + y, color); break;
        }
        if (err <= 0) { y++; err += 2*y + 1; }
        if (err > 0) { x--; err -= 2*x + 1; }
    }
}

void draw_rounded_rect(RoundedRectParams *params, uint16_t color) {
    // 绘制主体
    st7789v_spi_draw_area(params->x + params->radius, params->y, 
                         params->x + params->width - params->radius - 1, 
                         params->y + params->height - 1, color);
    // 绘制圆角
    draw_quarter_circle(params->x + params->radius, params->y + params->radius, 
                       params->radius, 2, color);
    draw_quarter_circle(params->x + params->width - params->radius - 1, 
                       params->y + params->radius, params->radius, 1, color);
    draw_quarter_circle(params->x + params->radius, 
                       params->y + params->height - params->radius - 1, 
                       params->radius, 3, color);
    draw_quarter_circle(params->x + params->width - params->radius - 1, 
                       params->y + params->height - params->radius - 1, 
                       params->radius, 4, color);
    // 填充空白
    st7789v_spi_draw_area(params->x, params->y + params->radius, 
                         params->x + params->width - 1, 
                         params->y + params->height - params->radius - 1, color);
}

void blink_animation(RoundedRectParams *rect, uint16_t frames, uint16_t delay_ms) {
    uint16_t original_height = rect->height;
    for(uint16_t i = 0; i < frames; i++) {
        // 闭合动画
        rect->height = original_height * (frames - i) / frames;
        st7789v_spi_draw_area(rect->x, rect->y, 
                             rect->x + rect->width - 1, 
                             rect->y + original_height - 1, BLACK);
        draw_rounded_rect(rect, WHITE);
        usleep(delay_ms * 1000);
        
        // 张开动画
        if(i < frames - 1) {
            rect->height = original_height * (i + 1) / frames;
            st7789v_spi_draw_area(rect->x, rect->y, 
                                 rect->x + rect->width - 1, 
                                 rect->y + original_height - 1, BLACK);
            draw_rounded_rect(rect, WHITE);
            usleep(delay_ms * 1000);
        }
    }
    rect->height = original_height;
}

int main() {
    st7789v_spi_init();
    st7789v_spi_draw_area(0, 0, SCREEN_WIDTH-1, SCREEN_HEIGHT-1, BLACK);

    RoundedRectParams rect1 = {SCREEN_WIDTH/4-40, SCREEN_HEIGHT/2-50, 80, 100, 10};
    RoundedRectParams rect2 = {SCREEN_WIDTH*3/4-40, SCREEN_HEIGHT/2-50, 80, 100, 10};

    while(1) {
        draw_rounded_rect(&rect1, WHITE);
        draw_rounded_rect(&rect2, WHITE);
        blink_animation(&rect1, 5, 100);
        blink_animation(&rect2, 5, 100);
        usleep(2000000); // 眨眼间隔2秒
    }
    return 0;
}
